package com.desire.test;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author desire
 */
public class _26_QRcode二维码 {

    public static void main(String[] args) throws Exception {
        String name = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\qr1.png";
        String content = "程序学习 2021";
        int width = 200;
        int height = 200;

        // 生成二维码
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 2);
        BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        MatrixToImageWriter.writeToStream(bm, "png", new FileOutputStream(name));


        // 将老的二维码上色 保存到新的文件
        BufferedImage bar = ImageIO.read(new File(name));
        BufferedImage ok = new BufferedImage(width, height, 1);
        ok.getGraphics().drawImage(bar, 0, 0, width, height, null);

        Random rand = new Random();
        Color c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                ok.setRGB(x, y, bar.getRGB(x, y) == -1 ? 0xffffffff : c.getRGB());
            }
        }
        String col = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\qr2.png";
        ImageIO.write(ok, "png", new File(col));

        //读取
        try {
            BufferedImage image = ImageIO.read(new File(name));
            BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
            HybridBinarizer bin = new HybridBinarizer(source);
            BinaryBitmap bitmap = new BinaryBitmap(bin);
            Result decode = new QRCodeReader().decode(bitmap);
            System.out.println(decode);
        } catch (IOException | ChecksumException | NotFoundException | FormatException e) {
            e.printStackTrace();
        }
    }

    public static void main1(String[] args) {
        String pathTo = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\qr.png";
        String contents = "压死了你连非凡";
        //生成二维码
        HashMap<EncodeHintType, Object> hint = new HashMap<>();
        hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hint.put(EncodeHintType.MARGIN, 2);
        try {
            BitMatrix bmx = new QRCodeWriter().encode(contents, BarcodeFormat.QR_CODE, 200, 200, hint);
            MatrixToImageWriter.writeToStream(bmx, "png", new FileOutputStream(pathTo));
        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }


        //读取二维码
        try {
            BufferedImage image = ImageIO.read(new File(pathTo));
            BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
            HybridBinarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap bitmap = new BinaryBitmap(binarizer);
            Result decode = new QRCodeReader().decode(bitmap);
            System.out.println(decode);
        } catch (IOException | ChecksumException | NotFoundException | FormatException e) {
            e.printStackTrace();
        }
    }
}
